using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Integratedes.Organization.DomainService;
using SOEI.Solution.Integratedes.Organization.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.Integratedes.Organization
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：XXX 模块<br/>
    /// 【创建日期】  ：2020.05.21 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [AbpAuthorize]
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = false)]
    public class OEconomyPersonChangeAppService : SolutionAppServiceBase, IOEconomyPersonChangeAppService
    {
        /// <summary>
        ///【OEconomyPersonChange】仓储层
        /// </summary>
        private readonly IRepository<OEconomyPersonChange, string> _oeconomypersonchangeRepository;

        /// <summary>
        ///【OEconomyPersonChange】领域服务
        /// </summary>
        private readonly IOEconomyPersonChangeManager _oeconomypersonchangeManager;

        public OEconomyPersonChangeAppService(
            IRepository<OEconomyPersonChange, string> oeconomypersonchangeRepository,
            IOEconomyPersonChangeManager oeconomypersonchangeManager
        )
        {
            _oeconomypersonchangeRepository = oeconomypersonchangeRepository;
            _oeconomypersonchangeManager = oeconomypersonchangeManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【OEconomyPersonChange】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<PagedResultDto<OEconomyPersonChangeListDto>> GetPaged(GetOEconomyPersonChangesInput input)
        {
            var query = _oeconomypersonchangeRepository.GetAll()
                          //模糊搜索 字段RYLXMC
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.RYLXMC.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<OEconomyPersonChangeListDto>>(entityList);

            return new PagedResultDto<OEconomyPersonChangeListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【OEconomyPersonChange】通过指定id获取MemberListDto信息
        /// </summary>

        public async Task<OEconomyPersonChangeListDto> GetById(EntityDto<string> input)
        {
            var entity = await _oeconomypersonchangeRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<OEconomyPersonChangeListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【Company】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<GetOEconomyPersonChangeForEditOutput> GetForEdit(EntityDto<string> input)
        {
            var output = new GetOEconomyPersonChangeForEditOutput();
            OEconomyPersonChangeEditDto editDto;

            if (!string.IsNullOrEmpty(input.Id))
            {
                var entity = await _oeconomypersonchangeRepository.GetAsync(input.Id);
                editDto = ObjectMapper.Map<OEconomyPersonChangeEditDto>(entity);
            }
            else
            {
                editDto = new OEconomyPersonChangeEditDto();
            }
            output.EconomyPerson = editDto;
            return output;
        }
        /// <summary>
        ///【OEconomyPersonChange】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task CreateOrUpdate(CreateOrUpdateOEconomyPersonChangeInput input)
        {
            if (!string.IsNullOrEmpty(input.OEconomyPersonChange.Id))
            {
                await Update(input.OEconomyPersonChange);
            }
            else
            {
                await Create(input.OEconomyPersonChange);
            }
        }
        /// <summary>
        ///【OEconomyPersonChange】新增
        /// </summary>

        protected virtual async Task<OEconomyPersonChangeEditDto> Create(OEconomyPersonChangeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<OEconomyPersonChange>(input);
            //调用领域服务
            entity = await _oeconomypersonchangeManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<OEconomyPersonChangeEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【OEconomyPersonChange】编辑
        /// </summary>

        protected virtual async Task Update(OEconomyPersonChangeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id;
            var entity = await _oeconomypersonchangeRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _oeconomypersonchangeManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【OEconomyPersonChange】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task Delete(EntityDto<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _oeconomypersonchangeManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【OEconomyPersonChange】 批量删除Member的方法
        /// </summary>

        public async Task BatchDelete(List<string> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _oeconomypersonchangeManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/
        #endregion
    }
}
